﻿using System;
using System.Web.Routing;
using System.Web;
using Dinamico.Service;

public static class RouteDataExtensions
{
    /// <summary>
    /// Get the <see cref="Const.Route.CategoryRouteDataKey"/> Value from the current RouteData.
    /// If not defined/exists, return NULL.
    /// </summary>
    /// <returns></returns>
    public static string GetCategoryValue(this RouteData routeData)
    {
        if (!routeData.Values.ContainsKey(Const.Route.CategoryRouteDataKey))
        {
            var category = HttpContext.Current.Session[Const.Session.CategorySessionKey];
            if (category != null)
            {
                return category.ToString();
            }
            else
            {
                return null;
                //throw new ArgumentException(Const.Route.CategoryRouteDataKey + " must be a parameter in Route definition or in Session, e.g: Articles/{category}/");
            }
        }
        else
        {
            return routeData.Values["category"].ToString();
        }
    }

    /// <summary>
    /// Returns a fully qualified URL for the specified route name.
    /// </summary>
    /// <param name="helper">HtmlHelper</param>
    /// <param name="Const_Route_Name"><see cref="Const.Routes"/> name.</param>
    /// <returns>Url</returns>
    public static string GetUrlForRoute(string Const_Route_Name)
    {
        if (String.IsNullOrEmpty(Const_Route_Name)) throw new ArgumentNullException("Const_Route_Name");

        var route = RouteTable.Routes[Const_Route_Name] as Route;

        return VirtualPathUtility.ToAbsolute("~/" + route.Url);
    }
}